from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait


class Base:
    # 初始化基类
    def __init__(self, driver: WebDriver = None):
        # 对driver进行复用，如果不存在driver，就构造一个新的
        if not driver:
            self.driver = webdriver.Edge()
            self.driver.implicitly_wait(2)
            self.driver.get("https://test-eu-leapcloud.leapmotor.com/")
            self.driver.maximize_window()  # 最大化窗口
        else:
            self.driver = driver

    def close(self):
        self.driver.quit()

    # 提取 find
    def base_find(self, by, locate):
        return WebDriverWait(self.driver, 10, 0.5).until(lambda x: self.driver.find_element(by, locate))

    # 提取 click
    def base_click(self, by, locate):
        return self.base_find(by, locate).click()

    # 提取 send_keys
    def base_send(self, by, locate, value):
        return self.base_find(by, locate).send_keys(value)

    # 提取 handles
    def base_handles(self, window):
        return self.driver.switch_to.window(window)
